﻿using System;
using NetBay.Core.BusinessEntities;
using Rio.Applications.Web.Services;

namespace Rio.Framework.Pages.CriteriaControls
{

    public class CriteriaFactory
    {
        /// <summary>
        /// Provide a factory of Criteria control with no selected value
        /// </summary>
        /// <param name="Index">The index.</param>
        /// <param name="FolderTypeIdentifier">The folder type identifier.</param>
        /// <param name="UseDefaultValue">if set to <c>true</c> [use default value].</param>
        /// <param name="IsSearch">if set to <c>true</c> [is search].</param>
        /// <returns></returns>
        public static CriteriaControlBase CreateControl(Index Index, long FolderTypeIdentifier, bool UseDefaultValue, bool IsSearch = false)
        {
            switch (Index.Type)
            {
                case (int) IndexDomainServiceEnums.IndexType.String:
                    return new TextIndexControl(Index, null, UseDefaultValue);
                
                case (int) IndexDomainServiceEnums.IndexType.Integer:
                    return new IntegerIndexControl(Index, null, UseDefaultValue);
                
                case (int) IndexDomainServiceEnums.IndexType.Double:
                    return new DoubleIndexControl(Index, null, UseDefaultValue);
                
                case (int) IndexDomainServiceEnums.IndexType.Date:
                    if (IsSearch)
                        return new DateIndexSearchControl(Index, null, UseDefaultValue);
                    return new DateIndexControl(Index, null, UseDefaultValue);
                
                case (int) IndexDomainServiceEnums.IndexType.Users:
                    return new ListUserIndexControl(Index);
                
                case (int) IndexDomainServiceEnums.IndexType.Boolean:
                    return new BooleanIndexControl(Index);
                
                case (int) IndexDomainServiceEnums.IndexType.IntegerString:
                    return new IntegerIndexControl(Index, null, UseDefaultValue);
                
                case (int) IndexDomainServiceEnums.IndexType.ListOfValues:
                    return new ListOjectIndexControl(Index, FolderTypeIdentifier, -1);
                
                case (int) IndexDomainServiceEnums.IndexType.DocumentType:
                    return new TextIndexControl(Index, null, UseDefaultValue);
                
                case (int) IndexDomainServiceEnums.IndexType.Groups:
                    return new ListGroupIndexControl(Index, null, UseDefaultValue);
                
                case (int) IndexDomainServiceEnums.IndexType.ListeOfRecipient:
                    return new ListGroupIndexControl(Index, null, UseDefaultValue);
                
                case (int) IndexDomainServiceEnums.IndexType.ListOfSenders:
                    return new ListGroupIndexControl(Index, null, UseDefaultValue);
                
                case (int) IndexDomainServiceEnums.IndexType.Object:
                    return new TextIndexControl(Index, null, UseDefaultValue);
                
                case (int) IndexDomainServiceEnums.IndexType.TypeFlow:
                    return new ListOjectIndexControl(Index, FolderTypeIdentifier, -1);
                
                case (int) IndexDomainServiceEnums.IndexType.TypeChrono:
                    return new TextIndexControl(Index, null, true) {IsEnabled = IsSearch};
                
                default:
                    return new TextIndexControl(Index, null, UseDefaultValue);
            }
        }

        /// <summary>
        /// Provide a factory of Criteria control
        /// </summary>
        /// <param name="Index">The index.</param>
        /// <param name="FolderTypeIdentifier">The folder type identifier.</param>
        /// <param name="SelectedValue">The selected value.</param>
        /// <param name="UseDefaultValue">if set to <c>true</c> [use default value].</param>
        /// <param name="IsSearch">if set to <c>true</c> [is search].</param>
        /// <returns></returns>
        public static CriteriaControlBase CreateControlWithValue(Index Index, long FolderTypeIdentifier, string SelectedValue, bool UseDefaultValue, bool IsSearch = false)
        {

            //For every value from ListValues, we have "VALUE;ID" for value list type index and user list type index, and we have "VALUE" for the other index type
            //because for value list type index and user list type index we need the value to display and id for identify the value (for the consultation screen for exemple, to select the item in the combobox) 
            //So here we display only the Value 
            string[] L_oTabValue = SelectedValue.Split(new[] { ';' });
            switch (Index.Type)
            {
                case (int)IndexDomainServiceEnums.IndexType.String:
                    {
                        string L_szcontrolValue = (L_oTabValue.Length > 0) ? L_oTabValue[0] : null;
                        return new TextIndexControl(Index, L_szcontrolValue, UseDefaultValue) { IsEnabled = true };
                       
                    }
                case (int)IndexDomainServiceEnums.IndexType.Integer:
                    {
                        string L_szcontrolValue = (L_oTabValue.Length > 0) ? L_oTabValue[0] : null;
                        return new IntegerIndexControl(Index, L_szcontrolValue, UseDefaultValue) { IsEnabled = true };
                       
                    }
                case (int)IndexDomainServiceEnums.IndexType.Double:
                    {
                        string L_szcontrolValue = (L_oTabValue.Length > 0) ? L_oTabValue[0] : null;
                        return new DoubleIndexControl(Index, L_szcontrolValue, UseDefaultValue) { IsEnabled = true };
                       
                    }
                case (int)IndexDomainServiceEnums.IndexType.Date:
                    {
                        //uncomment: 3.
                        string L_szcontrolValue = (L_oTabValue.Length > 0) ? L_oTabValue[0] : null;
                        if(!IsSearch)
                            return new DateIndexControl(Index, L_szcontrolValue, UseDefaultValue) { IsEnabled = true };
                        return new DateIndexSearchControl(Index, L_szcontrolValue, UseDefaultValue) { IsEnabled = true };
                    }
                case (int)IndexDomainServiceEnums.IndexType.Users:
                    {
                        //For value list we take the identifier (L_oTabValue[1]) and not the value (L_oTabValue[0] is for display)
                        string L_lSelectedValue = (L_oTabValue.Length > 1) ? L_oTabValue[1] : null;
                        return new ListUserIndexControl(Index, L_lSelectedValue) { IsEnabled = true };
                       
                    }
                case (int)IndexDomainServiceEnums.IndexType.Boolean:
                    {
                        string L_szcontrolValue = (L_oTabValue.Length > 0) ? L_oTabValue[0] : null;
                        return new BooleanIndexControl(Index, L_szcontrolValue) { IsEnabled = true };
                       
                    }
                case (int)IndexDomainServiceEnums.IndexType.IntegerString:
                    {
                        string L_szcontrolValue = (L_oTabValue.Length > 0) ? L_oTabValue[0] : null;
                        return new IntegerIndexControl(Index, L_szcontrolValue, UseDefaultValue) { IsEnabled = true };
                       
                    }
                case (int)IndexDomainServiceEnums.IndexType.ListOfValues:
                    {
                        //For value list we take the identifier (L_oTabValue[1]) and not the value (L_oTabValue[0] is for display)
                        ListOjectIndexControl L_oListOjectIndexControl = null;
                        if (L_oTabValue.Length == 2)
                        {
                            long? L_lSelectedValue = (L_oTabValue.Length > 1) ? (long?)Convert.ToInt64(L_oTabValue[1]) : null;
                            L_oListOjectIndexControl = new ListOjectIndexControl(Index, FolderTypeIdentifier, L_lSelectedValue) { IsEnabled = true };
                        }
                        else
                        {
                            string L_lSelectedValue = (L_oTabValue.Length > 0) ? L_oTabValue[0] : null;
                            L_oListOjectIndexControl = new ListOjectIndexControl(Index, FolderTypeIdentifier, -1, UseDefaultValue) { IsEnabled = true };
                            L_oListOjectIndexControl.SelectedValueAfterInit = L_lSelectedValue;
                        }
                        return L_oListOjectIndexControl;
                       
                    }
                case (int)IndexDomainServiceEnums.IndexType.DocumentType:
                    {
                        string L_szcontrolValue = (L_oTabValue.Length > 0) ? L_oTabValue[0] : null;
                        return new TextIndexControl(Index, L_szcontrolValue, UseDefaultValue) { IsEnabled = true };
                       
                    }
                case (int)IndexDomainServiceEnums.IndexType.Groups:
                case (int)IndexDomainServiceEnums.IndexType.ListeOfRecipient:
                case (int)IndexDomainServiceEnums.IndexType.ListOfSenders:
                    {
                        //For value list we take the identifier (L_oTabValue[1]) and not the value (L_oTabValue[0] is for display)
                        return new ListGroupIndexControl(Index, SelectedValue, UseDefaultValue) { IsEnabled = true };
                       
                    }
                case (int)IndexDomainServiceEnums.IndexType.Object:
                    {
                        string L_szcontrolValue = (L_oTabValue.Length > 0) ? L_oTabValue[0] : null;
                        return new TextIndexControl(Index, L_szcontrolValue, UseDefaultValue) { IsEnabled = true };
                       
                    }
                case (int)IndexDomainServiceEnums.IndexType.TypeFlow:
                    {
                        //For value list we take the identifier (L_oTabValue[1]) and not the value (L_oTabValue[0] is for display)
                        ListOjectIndexControl L_oListOjectIndexControl = null;
                        if (L_oTabValue.Length == 2)
                        {
                            long? L_lSelectedValue = (L_oTabValue.Length > 1) ? (long?)Convert.ToInt64(L_oTabValue[1]) : null;
                            L_oListOjectIndexControl = new ListOjectIndexControl(Index, FolderTypeIdentifier, L_lSelectedValue, UseDefaultValue);
                        }
                        else
                        {
                            string L_lSelectedValue = (L_oTabValue.Length > 0) ? L_oTabValue[0] : null;
                            L_oListOjectIndexControl = new ListOjectIndexControl(Index, FolderTypeIdentifier, -1) { IsEnabled = true };
                            L_oListOjectIndexControl.SelectedValueAfterInit = L_lSelectedValue;
                        }
                        return L_oListOjectIndexControl;
                       
                    }
                default:
                    {
                        string L_szcontrolValue = (L_oTabValue.Length > 0) ? L_oTabValue[0] : null;
                        return new TextIndexControl(Index, L_szcontrolValue, UseDefaultValue) { IsEnabled = true };
                       
                    }
            }
        }
    }
}
